package client.ui.theme.defaultTheme;

import server.Preferences;
import server.Preferences.ConversationWindow;
import server.Preferences.SortBy;
import client.engine.ChatEngine;
import client.ui.IPreferencesWindow;

/**
 * <h1>Class DefaultPreferencesWindow</h1>
 *
 * @author Tomas
 * @date March 14 2009
 */
public class DefaultPreferencesWindow extends javax.swing.JFrame implements IPreferencesWindow{

	private Preferences pref = null;
	private ChatEngine engine = null;
	
    public DefaultPreferencesWindow() {
        initComponents();
    }
                        
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        sortByCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        conversationCombo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        timeAwayText = new javax.swing.JTextField();
        soundCheck = new javax.swing.JCheckBox();
        offlineCheck = new javax.swing.JCheckBox();
        groupCheck = new javax.swing.JCheckBox();
        iconsCheck = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        pathText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        historyText = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Parlanchin - User Preferences");
        setResizable(false);

        jLabel1.setText("Sort by:");

        sortByCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "Group" }));

        jLabel2.setText("Show conversations:");

        conversationCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Individual", "Tabs" }));

        jLabel3.setText("Time to away mode:");

        soundCheck.setText("Sound");

        offlineCheck.setText("Show offline contacts");

        groupCheck.setText("Group contacts");

        iconsCheck.setText("Show icons");

        jLabel4.setText("Default path to store files:");

        jLabel5.setText("Kept history during (days):");

        historyText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(58, 58, 58)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(groupCheck)
                                    .addComponent(soundCheck))))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(offlineCheck)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(timeAwayText)
                                .addComponent(conversationCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(sortByCombo, 0, 140, Short.MAX_VALUE))
                            .addComponent(iconsCheck)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(508, Short.MAX_VALUE)
                        .addComponent(saveButton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(historyText, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                            .addComponent(pathText, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(sortByCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(conversationCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(timeAwayText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(soundCheck)
                    .addComponent(offlineCheck))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(groupCheck)
                    .addComponent(iconsCheck))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pathText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(historyText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addContainerGap())
        );

        pack();
    }                       

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
    	pref.setIcon(this.iconsCheck.isSelected());
    	pref.setGrouped(this.groupCheck.isSelected());
    	pref.setShowOffline(this.offlineCheck.isSelected());
    	pref.setSound(this.soundCheck.isSelected());
    	
    	pref.setDefaultPath(this.pathText.getText());
    	try{
    		pref.setTimeAwayMode(Long.parseLong(this.timeAwayText.getText()));
    		pref.setHistoryDays(Integer.parseInt(this.historyText.getText()));
    	}catch(Exception e){
    		
    	}
    	
    	String conv = (String) this.conversationCombo.getSelectedItem();
    	if(conv.equals("Individual")){
    		pref.setConvWindow(ConversationWindow.INDIVIDUAL);
    	}else{
    		pref.setConvWindow(ConversationWindow.TABBED);
    	}
    	
    	String sort = (String) this.sortByCombo.getSelectedItem();
    	if(sort.equals("Name")){
    		pref.setSortCriteria(SortBy.NAME);
    	}else{
    		pref.setSortCriteria(SortBy.GROUP);
    	}
    	
    	this.engine.storePreferences(pref);
    	
    	this.dispose();
    	
    }                                          

                   
    private javax.swing.JComboBox conversationCombo;
    private javax.swing.JCheckBox groupCheck;
    private javax.swing.JTextField historyText;
    private javax.swing.JCheckBox iconsCheck;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JCheckBox offlineCheck;
    private javax.swing.JTextField pathText;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox sortByCombo;
    private javax.swing.JCheckBox soundCheck;
    private javax.swing.JTextField timeAwayText;

	@Override
	public void displayPreferences(Preferences pref) {
		
		this.pref = pref;
		
		this.timeAwayText.setText("" + pref.getTimeAwayMode());
		this.pathText.setText(pref.getDefaultPath());
		this.historyText.setText("" + pref.getHistoryDays());
		
		this.soundCheck.setSelected(pref.isSoundActive());
		this.groupCheck.setSelected(pref.isGrouped());
		this.offlineCheck.setSelected(pref.isShowOffline());
		this.iconsCheck.setSelected(pref.isIconActive());
		
		switch(pref.getConvWindow()){
			case INDIVIDUAL:
				this.conversationCombo.setSelectedItem("Individual");
				break;
			case TABBED:
				this.conversationCombo.setSelectedItem("Tabs");
				break;
		}
		
		switch(pref.getSortCriteria()){
			case NAME:
				this.sortByCombo.setSelectedItem("Name");
				break;
			case GROUP:
				this.sortByCombo.setSelectedItem("Group");
				break;
		}
	}

	@Override
	public void setChatEngine(ChatEngine engine) {
		
		this.engine = engine;
		
	}

	@Override
	public void showWindow() {

		this.setVisible(true);
		
	}                

}
